GATE Exam  >  GATE Questions  >  Consider the C program below.#include int *A,... Start Learning for Free
Consider the C program below.
#include int *A, stkTop;
int stkFunc(int opcode, int val)
{
static int size=0, stkTop=0;
switch (opcode) {
case -1: size = val; break;
case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];
}
return -1;
}
int main()
{
int B[20]; A = B; stkTop = -1;
stkFunc (-1, 10);
stkFunc ( 0, 5);
stkFunc ( 0, 10);
printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));
}
The value printed by the above program is ___________.
  • a)
    5
  • b)
    10
  • c)
    15
  • d)
    20
Correct answer is option 'C'. Can you explain this answer?
Verified Answer
Consider the C program below.#include int *A, stkTop;int stkFunc(int o...
The code in main, basically initializes a stack of size 10, then pushes 5, then pushes 10. Finally the printf statement prints the sum of two pop operations which is 10 + 5 = 15.
stkFunc (-1, 10); // Initialize size as 10
stkFunc (0, 5); // push 5
stkFunc (0, 10); // push 10
// print sum of two pop
printf ("%d\n", stkFunc(1, 0) + stkFunc(1, 0));
View all questions of this test
Most Upvoted Answer
Consider the C program below.#include int *A, stkTop;int stkFunc(int o...
Understanding the Program Structure
The C program initializes a stack-like structure using a static array. Here's a breakdown of how it operates:
Global Variables
- `int *A, stkTop;` declares a pointer `A` and a variable `stkTop`.
- `A` will point to an integer array `B`, and `stkTop` tracks the current top of the stack.
Function Analysis: stkFunc
- The function `stkFunc` takes two parameters: `opcode` and `val`.
- Opcode -1: Sets the stack size.
- Opcode 0: Pushes `val` onto the stack if there is space.
- Default Case: Pops the top value from the stack if it's not empty.
Execution Flow in main()
1. Initialization:
- An array `B[20]` is created, and `A` points to it.
- `stkTop` is initialized to -1, indicating an empty stack.
2. Setting Stack Size:
- `stkFunc(-1, 10);` sets the stack size to 10.
3. Pushing Values:
- `stkFunc(0, 5);` pushes the value `5` onto the stack.
- `stkFunc(0, 10);` pushes the value `10` onto the stack.
4. Popping Values:
- The expression `stkFunc(1, 0) + stkFunc(1, 0)` calls the default case twice:
- The first call pops `10`.
- The second call pops `5`.
Final Calculation
- The sum of popped values: `10 + 5 = 15`.
Conclusion
Thus, the value printed by the program is 15, making option C the correct answer.
Explore Courses for GATE exam
Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer?
Question Description
Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer? for GATE 2024 is part of GATE preparation. The Question and answers have been prepared according to the GATE exam syllabus. Information about Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer? covers all topics & solutions for GATE 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer?.
Solutions for Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer? in English & in Hindi are available as part of our courses for GATE. Download more important topics, notes, lectures and mock test series for GATE Exam by signing up for free.
Here you can find the meaning of Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer?, a detailed solution for Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer? has been provided alongside types of Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the C program below.#include int *A, stkTop;int stkFunc(int opcode, int val){static int size=0, stkTop=0;switch (opcode) {case -1: size = val; break;case 0: if (stkTop < size) A[stkTop++] = val; break; default: if (stkTop) return A[--stkTop];}return -1;}int main(){int B[20]; A = B; stkTop = -1;stkFunc (-1, 10);stkFunc ( 0, 5);stkFunc ( 0, 10);printf ("%d\n", stkFunc(1, 0) + stkFunc(1,0));}The value printed by the above program is ___________.a)5b)10c)15d)20Correct answer is option 'C'. Can you explain this answer? tests, examples and also practice GATE tests.
Explore Courses for GATE exam
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev